package de.dhbw.asa.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * This class provides all the stuff we need for a Database Connection
 * @author aar
 *
 */
public class Database 
{
	
	public static final String DatabaseManager = null;
	public static String getDatabasemanager() {
		return DatabaseManager;
	}

	Connection jConnection;
	Statement jStatement;
	String sDatabaseName="jdbc:mysql://localhost:3306/webshop";
	String sUser="root";
	String sPassword="root";

	/*
	 * this method creates a Database Connection
	 */
	public boolean createConnection()
	{
		boolean bReturn = false;
		try
		{
			
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			jConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/webshop", "root", "root");
			jStatement = jConnection.createStatement();
			// Connection was created sucessfully
			bReturn = true;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return bReturn;
	}
	
	/*
	 * this method closes a given Connection
	 */
	public boolean closeConnection()
	{
		boolean bReturn = false;
		try
		{
			jConnection.close();
			// Connection was closed sucessfully
			bReturn = true;
			jStatement = null;
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return bReturn;
	}
	
	/*
	 * this method executes a SQLQuery
	 */
	public ResultSet executeQuery(String sQuery)
	{
		ResultSet rsReturn = null;
		try
		{
			rsReturn = jStatement.executeQuery(sQuery);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return rsReturn;
	}
	
	public void execute(String sQuery)
	{
		try
		{
			jStatement.execute(sQuery);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
}
